<template>
  <el-container>
    <el-header>
      <img class="logo" src="@/assets/logo.png">
      <el-menu
          :default-active="def_url"
          class="el-menu-demo"
          mode="horizontal"
          @select="handleSelect"
          background-color=rgb(53,61,64)
          text-color=rgb(233,238,243)
          active-text-color="#ffd04b">
        <el-menu-item index="1" @click="toHomePage">首页</el-menu-item>
        <el-menu-item index="2" @click="toAllBooks" >全部书籍</el-menu-item>
        <el-menu-item index="3" @click="toHotsale">热销</el-menu-item>
        <el-menu-item index="4" @click="toNewproduct">新品</el-menu-item>
        <el-menu-item index="5"  @click="toBacken">后台管理</el-menu-item>
        <el-menu-item index="6"  @click="toExit">退出</el-menu-item>
      </el-menu>
      <el-row :gutter="24">
        <el-col :span="14" >
          <el-input @keyup.enter.native="search" placeholder="请输入搜索信息">
            <i class="el-icon-search" slot="suffix" @click="search" style="line-height: 40px"></i>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button @click="toCart3" icon="el-icon-shopping-cart-2" circle></el-button>
        </el-col>
        <el-col :span="8">
          <div style="display:flex;margin-top: 5px;margin-left: 20px">
            <img style="width: 50px; height: 50px;border-radius: 50%;" :src="icon">
            <span  style="margin-left: 10px;">{{u_realname}}</span>
          </div>
        </el-col>
      </el-row>
    </el-header>
<el-main>
  <router-view></router-view>
</el-main>
    <el-footer>Footer</el-footer>
  </el-container>
</template>

<script>
export default {
  computed:{
    u_realname(){
      return sessionStorage.getItem("u_realname")
    },
    icon(){
      console.log(sessionStorage.getItem("icon"))
      return sessionStorage.getItem("icon")
    }
  },
  data() {
    return {
      def_url:'1'
    }
  },
  watch: {
    $route(to) {
      this.updateDefUrl(to.path);
    }
  },
  created() {
    this.updateDefUrl(this.$route.path);
  },
  methods: {
    search() {
    },
    toCart3(){
      if (this.$route.path !== '/adminhome/cart') {
        this.$router.push('/adminhome/cart')
      }
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      this.def_url = key;
      sessionStorage.setItem('def_url', key);
    },
    toHomePage(){
      if (this.$route.path !== '/adminhome/homepage') {
        this.$router.push('/adminhome/homepage');
      }
    },
    toAllBooks(){
      if (this.$route.path !== '/adminhome/allbooks') {
        this.$router.push('/adminhome/allbooks')
      }
    },
    toHotsale(){
      if (this.$route.path !== '/adminhome/hotsales') {
        this.$router.push('/adminhome/hotsales');
      }
    },
    toNewproduct(){
      if (this.$route.path !== '/adminhome/newproducts') {
        this.$router.push('/adminhome/newproducts');
      }
    },
    toBacken(){
      if (this.$route.path !== '/backendmanagement'){
        this.$router.push('/backendmanagement')
      }
    },
    toExit(){
      this.$router.push('/home/homepage')
    },
    updateDefUrl(path) {
      switch (path) {
        case '/adminhome/homepage':
          this.def_url = '1';
          break;
        case '/adminhome/allbooks':
          this.def_url = '2';
          break;
        case '/adminhome/hotsales':
          this.def_url = '3';
          break;
        case '/adminhome/newproducts':
          this.def_url = '4';
          break;
        case '/backendmanagement':
          this.def_url = '5';
        default:
          this.def_url = '1';
      }
      sessionStorage.setItem('def_url', this.def_url);
    }
  }
}
</script>

<style>
.logo{
  width: 200px;
  height: 60px;
  vertical-align: middle;
}
.el-container {
  height: 100vh;
  width: 100vw;
}

.el-header {
  display: flex;
  justify-content: space-between;
  background-color: rgb(53,61,64);
  color: white;
  text-align: center;
  line-height: 60px;
}

.el-footer {
  background-color: rgb(53,61,64);
  color: white;
  text-align: center;
  line-height: 60px;
}

.button-container {
  display: flex;
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  margin-left: 0;
  margin-top: 0;
}
.tx{
   width: 40px;
   height: 40px;
   border-radius: 50%;
   vertical-align: middle;
   margin-top: 13px;
 }

.userInfo{
  display: flex;

}
.userInfo>span{
  margin: auto 10px;
  color: white;
}
</style>
